package com.scuec.trainingmanagementsys.controller;

import com.scuec.trainingmanagementsys.entity.Project; // 假设 Project 类存在
import com.scuec.trainingmanagementsys.entity.TeamMember;

import com.scuec.trainingmanagementsys.service.TeamMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teamMember")
@CrossOrigin
public class TeamMemberController {

    @Autowired
    private TeamMemberService teamMemberService;

    /**
     * 插入团队成员信息
     * @param teamMember 团队成员对象
     * @return 插入结果信息
     */
    @PostMapping("/insert")
    public String insertTeamMember(@RequestBody TeamMember teamMember) {
        if (teamMemberService.insertTeamMember(teamMember)) {
            return "插入团队成员信息成功";
        } else {
            return "插入团队成员信息失败";
        }
    }

    /**
     * 将指定团队成员的角色更新为 Leader
     * @param teamMemberID 团队成员 ID
     * @return 更新结果信息
     */
    @PutMapping("/updateRole/{teamMemberID}")
    public String updateMemberRoleToLeader(@PathVariable Integer teamMemberID) {
        if (teamMemberService.updateMemberRoleToLeader(teamMemberID)) {
            return "更新团队成员角色为 Leader 成功";
        } else {
            return "更新团队成员角色为 Leader 失败";
        }
    }

    /**
     * 查询学生是否已经加入某个项目
     * @param studentId 学生ID
     * @param projectId 项目ID
     * @return 是否加入
     */
    @GetMapping("/isJoined/{studentId}/{projectId}")
    public boolean isStudentJoinedProject(@PathVariable Integer studentId, @PathVariable Integer projectId) {
        return teamMemberService.isStudentJoinedProject(studentId, projectId);
    }

    /**
     * 根据项目ID获取团队成员详情
     * @param projectId 项目ID
     * @return 团队成员详情列表
     */
    @GetMapping("/getTeamMembersByProjectId/{projectId}")
    public Map<String, Object> getTeamMembersByProjectId(@PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        List<TeamMember> teamMembers = teamMemberService.getTeamMembersByProjectId(projectId);
        result.put("code", 200);
        result.put("data", teamMembers);
        result.put("message", "success");
        return result;
    }

    /**
     * 根据学生ID获取学生加入的项目列表
     * @param studentId 学生ID
     * @return 项目列表
     */
    @GetMapping("/getJoinedProjects/{studentId}")
    public Map<String, Object> getJoinedProjects(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        List<Project> projects = teamMemberService.getJoinedProjects(studentId);
        result.put("code", 200);
        result.put("data", projects);
        result.put("message", "success");
        return result;
    }

    /**
     * 根据学生ID获取学生参与的项目列表
     * @param studentId 学生ID
     * @return 项目列表
     */
    @GetMapping("/getProjectsByStudentId/{studentId}")
    public Map<String, Object> getProjectsByStudentId(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        List<Project> projects = teamMemberService.getProjectsByStudentId(studentId);
        result.put("code", 200);
        result.put("data", projects);
        result.put("message", "success");
        return result;
    }
    /**
     * 根据学生ID获取学生参与的项目列表（包含指导老师姓名）
     * @param studentId 学生ID
     * @return 包含指导老师姓名的项目列表
     */
    @GetMapping("/getProjectsWithTeacher/{studentId}")
    public Map<String, Object> getProjectsWithTeacherByStudentId(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        List<Project> projects = teamMemberService.getProjectsWithTeacherByStudentId(studentId);
        result.put("code", 200);
        result.put("data", projects);
        result.put("message", "success");
        return result;
    }

    /**
     * 获取学生按项目的分布数据
     * @return 包含项目名称和学生数量的列表
     */
    @GetMapping("/studentDistributionByProject")
    public List<Map<String, Object>> getStudentDistributionByProject() {
        return teamMemberService.getStudentDistributionByProject();
    }

    /**
     * 根据项目ID获取团队成员详情，包括学生姓名和成员角色
     * @param projectId 项目ID
     * @return 团队成员详情列表
     */
    @GetMapping("/getTeamMembersDetailsByProjectId/{projectId}")
    public Map<String, Object> getTeamMembersDetailsByProjectId(@PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        List<TeamMember> teamMembers = teamMemberService.getTeamMembersDetailsByProjectId(projectId);
        result.put("code", 200);
        result.put("data", teamMembers);
        result.put("message", "success");
        return result;
    }

    /**
     * 更新团队成员的角色
     * @param teamMemberID 团队成员ID
     * @param memberRole 新的成员角色
     * @return 更新结果信息
     */
    @PutMapping("/updateMemberRole/{teamMemberID}/{memberRole}")
    public String updateMemberRole(@PathVariable Integer teamMemberID, @PathVariable String memberRole) {
        if (teamMemberService.updateMemberRole(teamMemberID, memberRole)) {
            return "更新团队成员角色成功";
        } else {
            return "更新团队成员角色失败";
        }
    }
    /**
     * 根据学生ID获取学生姓名
     * @param studentId 学生ID
     * @return 包含学生姓名的结果
     */
    @GetMapping("/getStudentName/{studentId}")
    public Map<String, Object> getStudentNameByStudentId(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        String studentName = teamMemberService.getStudentNameByStudentId(studentId);
        result.put("code", 200);
        result.put("data", studentName);
        result.put("message", "success");
        return result;
    }
    @GetMapping("/getTeamMembersByTeamId/{teamId}")
    public Map<String, Object> getTeamMembersByTeamId(@PathVariable Integer teamId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<TeamMember> teamMembers = teamMemberService.getTeamMembersByTeamId(teamId);
            result.put("code", 200);
            result.put("data", teamMembers);
            result.put("message", "success");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取小组成员失败");
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 根据学生 ID 获取 TeamMember
     * @param studentId 学生 ID
     * @return TeamMember 对象
     */
    @GetMapping("/getTeamMemberByStudentId/{studentId}")
    public Map<String, Object> getTeamMemberByStudentId(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        TeamMember teamMember = teamMemberService.getTeamMemberByStudentId(studentId);
        if (teamMember != null) {
            result.put("code", 200);
            result.put("data", teamMember);
            result.put("message", "success");
        } else {
            result.put("code", 404);
            result.put("message", "未找到对应的 TeamMember");
        }
        return result;
    }

    /**
     * 根据 TeamMemberID 获取 TeamID
     * @param teamMemberID TeamMember ID
     * @return 包含 TeamID 的结果
     */
    @GetMapping("/getTeamIdByTeamMemberId/{teamMemberID}")
    public Map<String, Object> getTeamIdByTeamMemberId(@PathVariable Integer teamMemberID) {
        Map<String, Object> result = new HashMap<>();
        Integer teamID = teamMemberService.getTeamIdByTeamMemberId(teamMemberID);
        if (teamID != null) {
            result.put("code", 200);
            // 使用 HashMap 封装 teamID
            Map<String, Integer> data = new HashMap<>();
            data.put("teamID", teamID);
            result.put("data", data);
            result.put("message", "success");
        } else {
            result.put("code", 404);
            result.put("message", "未找到对应的 TeamID");
        }
        return result;
    }
    @GetMapping("/getTeamIdByStudentId/{studentId}")
    public Map<String, Object> getTeamIdByStudentId(@PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer teamId = teamMemberService.getTeamIdByStudentId(studentId);
            if (teamId != null) {
                result.put("code", 200);
                Map<String, Integer> data = new HashMap<>();
                data.put("teamID", teamId);
                result.put("data", data);
                result.put("message", "success");
            } else {
                result.put("code", 404);
                result.put("message", "未找到该学生所在的小组");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取小组ID失败");
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("/getTeamIdByStudentAndProject/{studentId}/{projectId}")
    public Map<String, Object> getTeamIdByStudentAndProject(
            @PathVariable Integer studentId,
            @PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer teamId = teamMemberService.getTeamIdByStudentAndProject(studentId, projectId);
            if (teamId != null) {
                result.put("code", 200);
                result.put("data", teamId);
                result.put("message", "success");
            } else {
                result.put("code", 404);
                result.put("message", "未找到对应的小组");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取小组ID失败");
            e.printStackTrace();
        }
        return result;
    }
}

